import * as THREE from 'three';
import TWEEN from '../../../node_modules/three/examples/jsm/libs/tween.module.js';

//点移动动画
class pointAnimate{
    constructor(scene,pointStart,pointEnd,pointColor,pointSize,duration){
        this.scene = scene;
        this.pointStart = pointStart;
        this.pointEnd = pointEnd;
        this.pointSize = pointSize;
        this.duration = duration;
        this.color = pointColor;
        return this.init();
    }
    createPoint(){
        this.geometry = new THREE.BufferGeometry();
        this.geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( new THREE.Vector3(0,0,0), 3 ) );
        this.geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( this.color, 3 ) );
        this.material = new THREE.PointsMaterial( { size: this.pointSize, vertexColors: true } );
        this.point = new THREE.Points( this.geometry, this.material );
        this.point.position.set(this.pointStart.x,this.pointStart.y,this.pointStart.z);
        this.scene.add( this.point);
    }
    // 在动画更新时更新点位置
    updatePoint(p) {
        this.point.position.set(p.x,p.y,p.z);
        this.point.position.needsUpdate = true;
    }

    init(){
        this.createPoint();
        let currentPoint=this.pointStart.clone();
        return new TWEEN.Tween(currentPoint)
        .to({
            x: this.pointEnd.x,
            y: this.pointEnd.y,
            z: this.pointEnd.z
        }, this.duration)
        .onUpdate(() => {
            this.updatePoint(currentPoint);
        })
    }
}

export default pointAnimate;